package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.text.TextUtilsCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.adapter.PersonnelSettingAdapter;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemCheckedChangeListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 人员设置
 * Created by tony on 2017/12/21.
 */

public class PersonnelSettingListActivity extends Activity implements View.OnClickListener, ItemCheckedChangeListener {
    private RelativeLayout rl_right;
   // private ImageView img_right;
    private ImageView img_back;
    private TextView tv_title;
    private TextView btn_delete;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private List<String> selectedItemList = new ArrayList<String>();
    private ListView lv;
    private PersonnelSettingAdapter adapter;
    private final String TAG = "PersonnelSettingListActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_personnel_setting);

        init();
    }


    private void init() {
        img_back = findViewById(R.id.img_back);
        rl_right = findViewById(R.id.rl_right);
       // img_right = findViewById(R.id.img_right);
        tv_title = findViewById(R.id.tv_title);
        btn_delete = findViewById(R.id.btn_delete);
        lv = findViewById(R.id.lv);


        tv_title.setText("人员设置");
        img_back.setVisibility(View.VISIBLE);
        rl_right.setVisibility(View.VISIBLE);
      //  img_right.setBackgroundResource(R.mipmap.icon_msg);
        img_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        btn_delete.setBackgroundResource(R.mipmap.btn_long_bg_un);
        btn_delete.setClickable(false);
        btn_delete.setOnClickListener(this);

        adapter = new PersonnelSettingAdapter(this, TAG);
        adapter.setItemCheckedChangeListener(this);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);
    }


    @Override
    public void checkedChangeListener(int position, boolean isSlected) {
        String posStr = position + "";
        if (isSlected) {
            if (!selectedItemList.contains(posStr)) {
                selectedItemList.add(posStr);
            }
        } else {
            if (selectedItemList.contains(posStr)) {
                selectedItemList.remove(posStr);
            }
        }


        if (selectedItemList.size() > 0) {
            btn_delete.setBackgroundResource(R.mipmap.btn_long_bg_red);
            btn_delete.setClickable(true);
        } else {
            btn_delete.setBackgroundResource(R.mipmap.btn_long_bg_un);
            btn_delete.setClickable(false);
        }


    }

    @Override
    public void checkedChangeListener(View view, int position, boolean isSlected) {

    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;

            case R.id.rl_right: //人员设置
                mIntent = new Intent(PersonnelSettingListActivity.this, AddPersonnelSettingActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PERSONNEL_SETTING);

                break;
            case R.id.btn_delete:
//                if (selectedItemList.size() > 0) {
//
//                    for (String posStr : selectedItemList) {
//                        if (!TextUtils.isEmpty(posStr)) {
//                            int positon = Integer.parseInt(posStr);
//                            btn_log.setText("posStr" + posStr + " positon" + positon);
//                            if (positon >= 0 && positon < productBeanList.size()) {
//                                ProductBean bean = productBeanList.get(positon);
//                                btn_log.setText("posStr" + posStr + " positon" + positon + " bean " + bean.getProductName() + " benobu" + bean);
//                                productBeanList.remove(bean);
//                            }
//                        }
//
//                    }
//
//                    adapter.setData(productBeanList);
//                    adapter.notifyDataSetChanged();
//
//                    selectedItemList.clear();
//                    btn_delete.setBackgroundResource(R.mipmap.btn_long_bg_un);
//                    btn_delete.setClickable(false);


//                }
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        int i = 0;
        switch (requestCode) {
            case ConstantUtil.ADD_PERSONNEL_SETTING: //添加人员设置
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.PERSONNELSETTINGBEAN, false);
                    if (bool) {
//                        productBeanList.add(new ProductBean(i + "", i++, i + "", i + "", 0, null));
                        adapter.setData(productBeanList);
                        adapter.notifyDataSetChanged();
                    }

                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
